package org.jeecg.modules.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.CwJbszqkzb;
import org.jeecg.modules.cw.entity.CwJbszqkzbZhu;
import org.jeecg.modules.cw.entity.CwZyrybxcsmxbTj;
import org.jeecg.modules.cw.entity.CwZyrybxcsmxb;
import org.jeecg.modules.cw.mapper.CwZyrybxcsmxbMapper;
import org.jeecg.modules.cw.mapper.CwZyrybxcsmxbTjMapper;
import org.jeecg.modules.cw.service.ICwJbszqkzbService;
import org.jeecg.modules.cw.service.ICwJbszqkzbZhuService;
import org.jeecg.modules.cw.service.ICwZyrybxcsmxbTjService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 在职人员保险测算提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-09
 * @Version: V1.0
 */
@Service
public class CwZyrybxcsmxbTjServiceImpl extends ServiceImpl<CwZyrybxcsmxbTjMapper, CwZyrybxcsmxbTj> implements ICwZyrybxcsmxbTjService {

	@Autowired
	private CwZyrybxcsmxbTjMapper cwZyrybxcsmxbTjMapper;
	@Autowired
	private CwZyrybxcsmxbMapper cwZyrybxcsmxbMapper;
	@Autowired
	private ICwJbszqkzbService cwJbszqkzbService;
	@Autowired
	private ICwJbszqkzbZhuService cwJbszqkzbZhuService;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwZyrybxcsmxbTj cwZyrybxcsmxbTj, List<CwZyrybxcsmxb> cwZyrybxcsmxbList) {
		cwZyrybxcsmxbTjMapper.insert(cwZyrybxcsmxbTj);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		if(cwZyrybxcsmxbList!=null && cwZyrybxcsmxbList.size()>0) {

			for(CwZyrybxcsmxb entity:cwZyrybxcsmxbList) {
				if (entity.getPostWage()==null){
					entity.setPostWage(0.0);
				}else if (entity.getIncrementalStep()==null){
					entity.setIncrementalStep(0.0);
				}else if (entity.getLivingAllowance()==null){
					entity.setLivingAllowance(0.0);
				}else if (entity.getWorkAllowance()==null){
					entity.setWorkAllowance(0.0);
				}else if (entity.getSanitationFee()==null){
					entity.setSanitationFee(0.0);
				}else if (entity.getHnbt()==null){
					entity.setHnbt(0.0);
				}else if (entity.getJkbybt()==null){
					entity.setJkbybt(0.0);
				}else if (entity.getHealthSubsidies()==null){
					entity.setHealthSubsidies(0.0);
				}else if (entity.getHousingSubsidy()==null){
					entity.setHousingSubsidy(0.0);
				}else if (entity.getBpa()==null){
					entity.setBpa(0.0);
				}else if (entity.getZzyf()==null){
					entity.setZzyf(0);
				}else if (entity.getDebx()==null){
					entity.setDebx(0.0);
				}else if (entity.getYearAssess()==null){
					entity.setYearAssess(0.0);
				}
				entity.setMianId(cwZyrybxcsmxbTj.getId());
				if ("财政".equals(entity.getCzFcz())){
					//医疗保险缴费基数（年）
					double aaaTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getSanitationFee() + entity.getHnbt() + entity.getJkbybt()
							+ entity.getHealthSubsidies() + entity.getHousingSubsidy() + entity.getBpa()) - entity.getHealthSubsidies()) * entity.getZzyf();
					double aaa = new BigDecimal(aaaTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					//养老、失业、工伤保险缴费基数（年）
					double bbbbTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getHnbt() + entity.getJkbybt() + entity.getBpa()) *
							entity.getZzyf() + (entity.getPostWage() + entity.getIncrementalStep()) / 12 * entity.getZzyf());
					double bbbb = new BigDecimal(bbbbTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();

					System.out.println(aaa);
					System.out.println(bbbb);
					entity.setSsx(entity.getPostWage() + entity.getIncrementalStep());
					entity.setYsSyGs(bbbb);
					entity.setEndowmentInsurance(new BigDecimal(bbbb * 0.2).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setUnemploymentInsurance(new BigDecimal(bbbb * 0.005).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setGsbx(new BigDecimal(bbbb * 0.002).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setYlbxjs(aaa);
					entity.setMedicalInsurance(new BigDecimal(aaa * 0.12).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setCgyb(new BigDecimal(aaa * 0.05).setScale(2, RoundingMode.HALF_UP).doubleValue());
				}else {
					//医疗保险缴费基数（年）
					double aaaTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getSanitationFee() + entity.getHnbt() + entity.getJkbybt()
							+ entity.getHealthSubsidies() + entity.getHousingSubsidy() + entity.getBpa()) - entity.getHealthSubsidies()) * entity.getZzyf();
					double aaa = new BigDecimal(aaaTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					//养老、失业、工伤保险缴费基数（年）
					double bbbbTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getHnbt() + entity.getJkbybt() + entity.getBpa()) *
							entity.getZzyf() + (entity.getPostWage() + entity.getIncrementalStep()) / 12 * entity.getZzyf());
					double bbbb = new BigDecimal(bbbbTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					entity.setSsx(entity.getPostWage() + entity.getIncrementalStep());
					entity.setYsSyGs(bbbb);
					entity.setEndowmentInsurance(new BigDecimal(bbbb * 0.2).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setUnemploymentInsurance(new BigDecimal(bbbb * 0.005).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setGsbx(new BigDecimal(bbbb * 0.002).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setYlbxjs(aaa);
					entity.setMedicalInsurance(new BigDecimal(aaa * 0.12).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setCgyb(new BigDecimal(aaa * 0.05).setScale(2, RoundingMode.HALF_UP).doubleValue());

				}

				cwZyrybxcsmxbMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwZyrybxcsmxbTj cwZyrybxcsmxbTj,List<CwZyrybxcsmxb> cwZyrybxcsmxbList) {
		cwZyrybxcsmxbTjMapper.updateById(cwZyrybxcsmxbTj);

		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//			养老保险
		Double acz=0.0;
		Double afcz=0.0;
//			职工基本医疗保险缴费
		Double bcz=0.0;
		Double bfcz=0.0;
//			其他社会保障缴费
		Double ccz=0.0;
		Double cfcz=0.0;
		//1.先删除子表数据
		cwZyrybxcsmxbMapper.deleteByMainId(cwZyrybxcsmxbTj.getId());
		
		//2.子表数据重新插入
		if(cwZyrybxcsmxbList!=null && cwZyrybxcsmxbList.size()>0) {
			for(CwZyrybxcsmxb entity:cwZyrybxcsmxbList) {
				if (entity.getPostWage()==null){
					entity.setPostWage(0.0);
				}else if (entity.getIncrementalStep()==null){
					entity.setIncrementalStep(0.0);
				}else if (entity.getLivingAllowance()==null){
					entity.setLivingAllowance(0.0);
				}else if (entity.getWorkAllowance()==null){
					entity.setWorkAllowance(0.0);
				}else if (entity.getSanitationFee()==null){
					entity.setSanitationFee(0.0);
				}else if (entity.getHnbt()==null){
					entity.setHnbt(0.0);
				}else if (entity.getJkbybt()==null){
					entity.setJkbybt(0.0);
				}else if (entity.getHealthSubsidies()==null){
					entity.setHealthSubsidies(0.0);
				}else if (entity.getHousingSubsidy()==null){
					entity.setHousingSubsidy(0.0);
				}else if (entity.getBpa()==null){
					entity.setBpa(0.0);
				}else if (entity.getZzyf()==null){
					entity.setZzyf(0);
				}else if (entity.getDebx()==null){
					entity.setDebx(0.0);
				}else if (entity.getYearAssess()==null){
					entity.setYearAssess(0.0);
				}
				//外键设置
				entity.setMianId(cwZyrybxcsmxbTj.getId());
				if ("财政".equals(entity.getCzFcz())){
					//医疗保险缴费基数（年）
					double aaaTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getSanitationFee() + entity.getHnbt() + entity.getJkbybt()
							+ entity.getHealthSubsidies() + entity.getHousingSubsidy() + entity.getBpa()) - entity.getHealthSubsidies()) * entity.getZzyf();
					double aaa = new BigDecimal(aaaTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					//养老、失业、工伤保险缴费基数（年）
					double bbbbTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getHnbt() + entity.getJkbybt() + entity.getBpa()) *
							entity.getZzyf() + (entity.getPostWage() + entity.getIncrementalStep()) / 12 * entity.getZzyf());
					double bbbb = new BigDecimal(bbbbTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					entity.setSsx(entity.getPostWage() + entity.getIncrementalStep());
					entity.setYsSyGs(bbbb);
					entity.setEndowmentInsurance(new BigDecimal(bbbb * 0.2).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setUnemploymentInsurance(new BigDecimal(bbbb * 0.005).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setGsbx(new BigDecimal(bbbb * 0.002).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setYlbxjs(aaa);
					entity.setMedicalInsurance(new BigDecimal(aaa * 0.12).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setCgyb(new BigDecimal(aaa * 0.05).setScale(2, RoundingMode.HALF_UP).doubleValue());
					//其他社会保障缴费
					double current2 = aaa+(bbbb*0.005)+(bbbb*0.002)+bbbb;
					ccz = new BigDecimal(ccz).add(new BigDecimal(current2))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
					// 计算当前迭代的养老保险金额
					double current = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getHnbt() + entity.getJkbybt() + entity.getBpa()) *
							entity.getZzyf() + (entity.getPostWage() + entity.getIncrementalStep()) / 12 * entity.getZzyf()) * 0.2;
					//职工基本医疗保险缴费
					double current1=entity.getDebx()+(aaa*0.12)+(aaa*0.05);
					// 累加并保留两位小数
					acz = new BigDecimal(acz).add(new BigDecimal(current))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
						bcz = new BigDecimal(bcz).add(new BigDecimal(current1))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();


				}else {
					//医疗保险缴费基数（年）
					double aaaTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getSanitationFee() + entity.getHnbt() + entity.getJkbybt()
							+ entity.getHealthSubsidies() + entity.getHousingSubsidy() + entity.getBpa()) - entity.getHealthSubsidies()) * entity.getZzyf();
					double aaa = new BigDecimal(aaaTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					//养老、失业、工伤保险缴费基数（年）
					double bbbbTemp = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getHnbt() + entity.getJkbybt() + entity.getBpa()) *
							entity.getZzyf() + (entity.getPostWage() + entity.getIncrementalStep()) / 12 * entity.getZzyf());
					double bbbb = new BigDecimal(bbbbTemp).setScale(2, RoundingMode.HALF_UP).doubleValue();
					entity.setSsx(entity.getPostWage() + entity.getIncrementalStep());
					entity.setYsSyGs(bbbb);
					entity.setEndowmentInsurance(new BigDecimal(bbbb * 0.2).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setUnemploymentInsurance(new BigDecimal(bbbb * 0.005).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setGsbx(new BigDecimal(bbbb * 0.002).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setYlbxjs(aaa);
					entity.setMedicalInsurance(new BigDecimal(aaa * 0.12).setScale(2, RoundingMode.HALF_UP).doubleValue());
					entity.setCgyb(new BigDecimal(aaa * 0.05).setScale(2, RoundingMode.HALF_UP).doubleValue());
					//其他社会保障缴费
					double current2 = aaa+(bbbb*0.005)+(bbbb*0.002)+bbbb;
					cfcz = new BigDecimal(cfcz).add(new BigDecimal(current2))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
					// 计算当前迭代的养老保险金额
					double current = ((entity.getPostWage() + entity.getIncrementalStep() + entity.getLivingAllowance() +
							entity.getWorkAllowance() + entity.getHnbt() + entity.getJkbybt() + entity.getBpa()) *
							entity.getZzyf() + (entity.getPostWage() + entity.getIncrementalStep()) / 12 * entity.getZzyf()) * 0.2;
					// 累加并保留两位小数

					double current1=entity.getDebx()+(aaa*0.12)+(aaa*0.05);
					afcz = new BigDecimal(afcz).add(new BigDecimal(current))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
					bfcz = new BigDecimal(bfcz).add(new BigDecimal(current1))
							.setScale(2, BigDecimal.ROUND_HALF_UP)
							.doubleValue();
				}
				cwZyrybxcsmxbMapper.insert(entity);
			}
//			LambdaQueryWrapper<CwJbszqkzb> wrapper = new LambdaQueryWrapper<>();
//			wrapper.eq(CwJbszqkzb::getYear,cwZyrybxcsmxbTj.getYear())
//					.eq(CwJbszqkzb::getUnit,cwZyrybxcsmxbTj.getUnit())
//					.in(CwJbszqkzb::getProject,"机关事业单位基本养老保险缴费");
//			long count = cwJbszqkzbService.count(wrapper);
//			if (count == 1){
//				CwJbszqkzb one1 = cwJbszqkzbService.getOne(wrapper);
//				one1.setUnit(cwZyrybxcsmxbTj.getUnit());
//				one1.setYear(cwZyrybxcsmxbTj.getYear());
//				one1.setFinancial(acz);
//				one1.setNonFinancials(afcz);
//				if (one1.getYsshj()==null){
//					one1.setYsshj(0.0);
//				}
//				one1.setYsshj(acz+afcz);
//				one1.setCwPx(17);
//				cwJbszqkzbService.updateById(one1);
//			}
//
//			LambdaQueryWrapper<CwJbszqkzb> wrapper1 = new LambdaQueryWrapper<>();
//			wrapper1.eq(CwJbszqkzb::getYear,cwZyrybxcsmxbTj.getYear())
//					.eq(CwJbszqkzb::getUnit,cwZyrybxcsmxbTj.getUnit())
//					.in(CwJbszqkzb::getProject,"职工基本医疗保险缴费");
//			long count1 = cwJbszqkzbService.count(wrapper1);
//			if (count1 == 1) {
//				CwJbszqkzb one1 = cwJbszqkzbService.getOne(wrapper1);
//				one1.setUnit(cwZyrybxcsmxbTj.getUnit());
//				one1.setYear(cwZyrybxcsmxbTj.getYear());
//				one1.setFinancial(bcz);
//				one1.setNonFinancials(bfcz);
//				if (one1.getYsshj()==null){
//					one1.setYsshj(0.0);
//				}
//				one1.setYsshj(bcz+bfcz);
//				one1.setCwPx(19);
//				cwJbszqkzbService.updateById(one1);
//			}
//			LambdaQueryWrapper<CwJbszqkzb> wrapper2 = new LambdaQueryWrapper<>();
//			wrapper2.eq(CwJbszqkzb::getYear,cwZyrybxcsmxbTj.getYear())
//					.eq(CwJbszqkzb::getUnit,cwZyrybxcsmxbTj.getUnit())
//					.in(CwJbszqkzb::getProject,"其他社会保障缴费");
//			long count2 = cwJbszqkzbService.count(wrapper2);
//			if (count2 == 1){
//				CwJbszqkzb one1 = cwJbszqkzbService.getOne(wrapper2);
//				one1.setUnit(cwZyrybxcsmxbTj.getUnit());
//				one1.setYear(cwZyrybxcsmxbTj.getYear());
//				one1.setFinancial(ccz);
//				one1.setNonFinancials(cfcz);
//				if (one1.getYsshj()==null){
//					one1.setYsshj(0.0);
//				}
//				one1.setYsshj(ccz+cfcz);
//				one1.setCwPx(20);
//
//				cwJbszqkzbService.updateById(one1);
//			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwZyrybxcsmxbMapper.deleteByMainId(id);
		cwZyrybxcsmxbTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwZyrybxcsmxbMapper.deleteByMainId(id.toString());
			cwZyrybxcsmxbTjMapper.deleteById(id);
		}
	}
	
}
